/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.faculty;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author FRIENDS
 */
@WebServlet(name = "addquestion", urlPatterns = {"/faculty/exam/addquestion"})
public class addquestion extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
       
       int qno=Integer.parseInt(request.getParameter("qno"));
       String courseid=request.getParameter("courseid");
       String examid=request.getSession().getAttribute("examid").toString();
       String question=request.getParameter("question");
       String opt1=request.getParameter("opt1");
       String opt2=request.getParameter("opt2");
       String opt3=request.getParameter("opt3");
       String opt4=request.getParameter("opt4");
       int answer=Integer.parseInt(request.getParameter("answer"));
        try {
            PreparedStatement p=dbconnector.DBDriver.getPrepare("insert into "+examid+" values (?,?,?,?,?,?,?)");
            p.setInt(1, qno);
            p.setString(2, question);
            p.setString(3, opt1);
            p.setString(4, opt2);
            p.setString(5, opt3);
            p.setString(6, opt4);
            p.setInt(7, answer);
            int row=p.executeUpdate();
           
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if(request.getParameter("next")!=null)
         response.sendRedirect("preparepaper.jsp?&qno="+(++qno));
        else if(request.getParameter("finish")!=null)
        {
            request.getSession().removeAttribute("examid");
            response.sendRedirect("../classes/showcourses.jsp?status=0");
        }
    }

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
